package cn.yunyichina.provider.register.service.impl;

import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.dubbo.service.CacheService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.register.constant.CacheConstant;
import org.apache.commons.lang.StringUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 挂号缓存服务
 * Created by Administrator on 2017/5/10.
 */
@Deprecated
@Service
public class RegCacheServiceImpl {

    /**
     * 更新医院挂号规则缓存
     * yyt.register.reg.cache.rule.update
     * @param data
     * @return
     */
    public Response updateRegCacheRule(String data,String sessionId){
        Response response = new Response();
        Map<String, String> map = JsonUtils.parseObject(data, Map.class);
        String attribute_type = "2";
        map.put("attribute_type",attribute_type);
        String org_code = map.get("org_code");

        if(StringUtils.isBlank(org_code)){
            response.setResultCodeAndMessage("6101", "医院id不能为空");
            return response;
        }

        Response baseResponse = BaseService.callUnderlineFromInternal(sessionId,
                "yyt.base.rule.list.get", JsonUtils.toJsonString(map));
        if(!"0".equals(baseResponse.getResultCode())){
            response.setResultCodeAndMessage("6101", "查询规则出错");
            return response;
        }
        if(StringUtils.isNotBlank(baseResponse.getResult())){
            Response cacheResponse = CacheService.setGlobalKey(CacheConstant.CACHE_REGISTER_MODULE,CacheConstant.CACHE_REGISTER_RULE,
                    org_code,baseResponse.getResult(),0);
            if("0".equals(cacheResponse.getResultCode())) {
                response.setResultCodeAndMessageAndResult("0","缓存更新成功",baseResponse.getResult());
            }else {
                response.setResultCodeAndMessage(cacheResponse.getResultCode(),cacheResponse.getResultMessage());
            }
        }else {
            response.setResultCodeAndMessage(baseResponse.getResultCode(),baseResponse.getResultMessage());
        }

        return response;
    }

    /**
     *
     * @methodName:yyt.register.reg.cache.rule.get
     * @description:根据医院编码，获取医院挂号规则缓存
     * @author:wwb
     * @date:2017年5月10日
     * @param data
     * @return:Response
     */
    public Response getRegCacheRule(String data){
        Response response = new Response();
        Map<String, String> map = JsonUtils.parseObject(data, Map.class);
        String org_code = map.get("org_code");
        if(StringUtils.isBlank(org_code)){
            response.setResultCodeAndMessage("61101", "医院id不能为空");
            return response;
        }
        String cacheValue = CacheService.getGlobalKey(CacheConstant.CACHE_REGISTER_MODULE,CacheConstant.CACHE_REGISTER_RULE,org_code);
        if(org.apache.commons.lang.StringUtils.isNotBlank(cacheValue)) {
            response.setResultCodeAndMessageAndResult("0","查询成功",cacheValue);
        }else {
            response.setResultCodeAndMessage("61101","请先设置规则到缓存中");
        }
        return response;
    }

    /**
     * 更新挂号医院列表缓存，根据平台应用编码
     * yyt.register.reg.cache.hospital.update
     * @param data app_code如果传all,则缓存所有医院
     * @return
     */
    public Response updateRegCacheHospital(String data,String sessionId){
        Response response = new Response();
        Map<String, String> map = JsonUtils.parseObject(data, Map.class);
        String app_code = map.get("app_code");
        if(StringUtils.isBlank(app_code)){
            response.setResultCodeAndMessage("6101", "app_code不能为空");
            return response;
        }

        Response baseResponse = BaseService.callUnderlineFromInternal(sessionId,
                "yyt.base.hospital.by.app.code.list", JsonUtils.toJsonString(map));
        if(!"0".equals(baseResponse.getResultCode())){
            response.setResultCodeAndMessage("6101", "查询yyt.base.hospital.by.app.code.list出错");
            return response;
        }
        if(StringUtils.isNotBlank(baseResponse.getResult())){
            Response cacheResponse = CacheService.setGlobalKey(CacheConstant.CACHE_REGISTER_MODULE,CacheConstant.CACHE_REGISTER_HOSPITAL,
                    app_code,baseResponse.getResult(),0);
            if("0".equals(cacheResponse.getResultCode())) {
                response.setResultCodeAndMessageAndResult("0","缓存更新成功",baseResponse.getResult());
            }else {
                response.setResultCodeAndMessage(cacheResponse.getResultCode(),cacheResponse.getResultMessage());
            }
        }else {
            response.setResultCodeAndMessage(baseResponse.getResultCode(),baseResponse.getResultMessage());
        }

        return response;
    }

    /**
     *
     * @methodName:yyt.register.reg.cache.hospital.list
     * @description:根据医院编码，获取挂号医院列表缓存
     * @author:wwb
     * @date:2017年5月10日
     * @param data app_code如果传all,则查询所有医院
     * @return:Response
     */
    public Response listRegCacheHospital(String data){
        Response response = new Response();
        Map<String, String> map = JsonUtils.parseObject(data, Map.class);
        String app_code = map.get("app_code");
        if(StringUtils.isBlank(app_code)){
            response.setResultCodeAndMessage("6101", "app_code不能为空");
            return response;
        }

        String cacheValue = CacheService.getGlobalKey(CacheConstant.CACHE_REGISTER_MODULE,CacheConstant.CACHE_REGISTER_HOSPITAL,app_code);
        if(org.apache.commons.lang.StringUtils.isNotBlank(cacheValue)) {
            response.setResultCodeAndMessageAndResult("0","查询成功",cacheValue);
        }else {
            response.setResultCodeAndMessage("61101","请先更新医院列表到缓存中");
        }
        return response;
    }
}
